home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 2 / AACD 2.iso / AACD / Programming / fpc / amigaunits / realtime.pas < prev    next >
Pascal/Delphi Source File  |  1998-09-22  |  11KB  |  360 lines

  1. {
  2.     This file is part of the Free Pascal run time library.
  3.  
  4.     A file in Amiga system run time library.
  5.     Copyright (c) 1998 by Nils Sjoholm
  6.     member of the Amiga RTL development team.
  7.  
  8.     See the file COPYING.FPC, included in this distribution,
  9.     for details about the copyright.
  10.  
  11.     This program is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14.  
  15.  **********************************************************************}
  16.  
  17. UNIT realtime;
  18.  
  19. INTERFACE
  20. USES exec, utility;
  21.  
  22. {***************************************************************************}
  23.  
  24. const
  25. { realtime.library's idea of time is based on a clock which emits a pulse
  26.  * 1200 times a second (1.2kHz). All time values maintained by realtime.library
  27.  * are based on this number. For example, the field RealTimeBase->rtb_Time
  28.  * expresses an amount of time equivalent to (RealTimeBase->rtb_Time/TICK_FREQ)
  29.  * seconds.
  30.  }
  31.  TICK_FREQ = 1200;
  32.  
  33.  
  34. {***************************************************************************}
  35.  
  36.  
  37. { Each Conductor represents a group of applications which wish to remain
  38.  * synchronized together.
  39.  *
  40.  * This structure must only be allocated by realtime.library and is
  41.  * READ-ONLY!
  42.  }
  43. Type
  44.  pConductor = ^tConductor;
  45.  tConductor = record
  46.     cdt_Link        : tNode;
  47.     cdt_Reserved0   : WORD;
  48.     cdt_Players     : tMinList;          { this conductor's players      }
  49.     cdt_ClockTime,                       { current time of this sequence }
  50.     cdt_StartTime,                       { start time of this sequence   }
  51.     cdt_ExternalTime,                    { time from external unit       }
  52.     cdt_MaxExternalTime,                 { upper limit on sync'd time    }
  53.     cdt_Metronome   : ULONG;             { MetricTime highest pri node   }
  54.     cdt_Reserved1   : WORD;
  55.     cdt_Flags       : WORD;              { conductor flags               }
  56.     cdt_State       : Byte;              { playing or stopped            }
  57.  end;
  58.  
  59. const
  60. { Flag bits for Conductor.cdt_Flags }
  61.  CONDUCTF_EXTERNAL = 1;   { clock is externally driven }
  62.  CONDUCTF_GOTTICK  = 2;   { received 1st external tick }
  63.  CONDUCTF_METROSET = 4;   { cdt_Metronome filled in    }
  64.  CONDUCTF_PRIVATE  = 8;   { conductor is private       }
  65.  
  66.  CONDUCTB_EXTERNAL = 0;
  67.  CONDUCTB_GOTTICK  = 1;
  68.  CONDUCTB_METROSET = 2;
  69.  CONDUCTB_PRIVATE  = 3;
  70.  
  71. { constants for Conductor.cdt_State and SetConductorState() }
  72.  CONDSTATE_STOPPED    = 0;   { clock is stopped              }
  73.  CONDSTATE_PAUSED     = 1;   { clock is paused               }
  74.  CONDSTATE_LOCATE     = 2;   { go to 'running' when ready    }
  75.  CONDSTATE_RUNNING    = 3;   { run clock NOW                 }
  76.  
  77. { These do not actually exist as Conductor states, but are used as additional
  78.  * arguments to SetConductorState()
  79.  }
  80.  CONDSTATE_METRIC     = -1;   { ask high node to locate       }
  81.  CONDSTATE_SHUTTLE    = -2;   { time changing but not running }
  82.  CONDSTATE_LOCATE_SET = -3;   { maestro done locating         }
  83.  
  84.  
  85. {***************************************************************************}
  86.  
  87.  
  88. { The Player is the connection between a Conductor and an application.
  89.  *
  90.  * This structure must only be allocated by realtime.library and is
  91.  * READ-ONLY!
  92.  }
  93. Type
  94.  pPlayer = ^tPlayer;
  95.  tPlayer = record
  96.     pl_Link             : tNode;
  97.     pl_Reserved0,
  98.     pl_Reserved1        : Shortint;
  99.     pl_Hook             : pHook;         { player's hook function       }
  100.     pl_Source           : pConductor;    { pointer to parent context    }
  101.     pl_Task             : pTask;         { task to signal for alarm     }
  102.     pl_MetricTime       : Longint;       { current time in app's metric }
  103.     pl_AlarmTime        : Longint;       { time to wake up              }
  104.     pl_UserData         : Pointer;       { for application use  }
  105.     pl_PlayerID         : WORD;          { for application use  }
  106.     pl_Flags            : WORD;          { general Player flags         }
  107.  end;
  108.  
  109. const
  110. { Flag bits for Player.pl_Flags }
  111.  PLAYERF_READY     = 1;   { player is ready to go!        }
  112.  PLAYERF_ALARMSET  = 2;   { alarm is set                  }
  113.  PLAYERF_QUIET     = 3;   { a dummy player, used for sync }
  114.  PLAYERF_CONDUCTED = 8;   { give me metered time          }
  115.  PLAYERF_EXTSYNC   = 16;   { granted external sync         }
  116.  
  117.  PLAYERB_READY     = 0;
  118.  PLAYERB_ALARMSET  = 1;
  119.  PLAYERB_QUIET     = 2;
  120.  PLAYERB_CONDUCTED = 3;
  121.  PLAYERB_EXTSYNC   = 4;
  122.  
  123.  
  124. {***************************************************************************}
  125.  
  126.  
  127. { Tags for CreatePlayer(), SetPlayerAttrs(), and GetPlayerAttrs() }
  128.  PLAYER_Base         = (TAG_USER+64)   ;
  129.  PLAYER_Hook         = (PLAYER_Base+1) ;  { set address of hook function }
  130.  PLAYER_Name         = (PLAYER_Base+2) ;  { name of player       }
  131.  PLAYER_Priority     = (PLAYER_Base+3) ;  { priority of player           }
  132.  PLAYER_Conductor    = (PLAYER_Base+4) ;  { set conductor for player     }
  133.  PLAYER_Ready        = (PLAYER_Base+5) ;  { the "ready" flag             }
  134.  PLAYER_AlarmTime    = (PLAYER_Base+12);  { alarm time (sets PLAYERF_ALARMSET) }
  135.  PLAYER_Alarm        = (PLAYER_Base+13);  { sets/clears PLAYERF_ALARMSET flag  }
  136.  PLAYER_AlarmSigTask = (PLAYER_Base+6) ;  { task to signal for alarm/notify    }
  137.  PLAYER_AlarmSigBit  = (PLAYER_Base+8) ;  { signal bit for alarm (or -1) }
  138.  PLAYER_Conducted    = (PLAYER_Base+7) ;  { sets/clears PLAYERF_CONDUCTED flag   }
  139.  PLAYER_Quiet        = (PLAYER_Base+9) ;  { don't process time thru this }
  140.  PLAYER_UserData     = (PLAYER_Base+10);
  141.  PLAYER_ID           = (PLAYER_Base+11);
  142.  PLAYER_ExtSync      = (PLAYER_Base+14);  { attempt/release to ext sync  }
  143.  PLAYER_ErrorCode    = (PLAYER_Base+15);  { error return value           }
  144.  
  145.  
  146. {***************************************************************************}
  147.  
  148.  
  149. { Method types for messages sent via a Player's hook }
  150.  PM_TICK     = 0;
  151.  PM_STATE    = 1;
  152.  PM_POSITION = 2;
  153.  PM_SHUTTLE  = 3;
  154.  
  155. Type
  156. { used for PM_TICK, PM_POSITION and PM_SHUTTLE methods }
  157.  ppmTime = ^tpmTime;
  158.  tpmTime = record
  159.     pmt_Method  : ULONG;        { PM_TICK, PM_POSITION, or PM_SHUTTLE }
  160.     pmt_Time    : ULONG;
  161.  end;
  162.  
  163. { used for the PM_STATE method }
  164.  ppmState = ^tpmState;
  165.  tpmState = record
  166.     pms_Method  : ULONG;        { PM_STATE }
  167.     pms_OldState: ULONG;
  168.  end;
  169.  
  170.  
  171. {***************************************************************************}
  172.  
  173. const
  174. { Possible lock types for LockRealTime() }
  175.  RT_CONDUCTORS = 0;   { conductor list }
  176.  
  177.  
  178. {***************************************************************************}
  179.  
  180.  
  181. { realtime.library error codes }
  182.  RTE_NOMEMORY    = 801;   { memory allocation failed      }
  183.  RTE_NOCONDUCTOR = 802;   { player needs a conductor      }
  184.  RTE_NOTIMER     = 803;   { timer (CIA) allocation failed }
  185.  RTE_PLAYING     = 804;   { can't shuttle while playing   }
  186.  
  187.  
  188. {***************************************************************************}
  189.  
  190.  
  191. { OpenLibrary("realtime.library",0) returns a pointer to this structure.
  192.  * All fields are READ-ONLY.
  193.  }
  194. Type
  195.  pRealTimeBase = ^tRealTimeBase;
  196.  tRealTimeBase = record
  197.     rtb_LibNode     : tLibrary;
  198.     rtb_Reserved0   : Array[0..1] of Byte;
  199.  
  200.     rtb_Time,                      { current time                         }
  201.     rtb_TimeFrac    : ULONG;       { fixed-point fraction part of time    }
  202.     rtb_Reserved1   : WORD;
  203.     rtb_TickErr     : Integer;     { nanosecond error from ideal Tick     }
  204.  end;                              { length to real tick length           }
  205.  
  206. { Actual tick length is: 1/TICK_FREQ + rtb_TickErr/1e9 }
  207.  
  208. const
  209.  RealTime_TickErr_Min = -705;
  210.  RealTime_TickErr_Max =  705;
  211.  
  212. {*--- functions in V37 or higher (Release 2.04) ---*}
  213.  
  214. VAR RealTimeBase : pRealTimeBase;
  215.  
  216. FUNCTION CreatePlayerA(tagList : pTagItem) : pPlayer;
  217. PROCEDURE DeletePlayer(player : pPlayer);
  218. FUNCTION ExternalSync(player : pPlayer; minTime : LONGINT; maxTime : LONGINT) : BOOLEAN;
  219. FUNCTION FindConductor(name : pCHAR) : pConductor;
  220. FUNCTION GetPlayerAttrsA(player : pPlayer; tagList : pTagItem) : ULONG;
  221. FUNCTION LockRealTime(lockType : ULONG) : POINTER;
  222. FUNCTION NextConductor(previousConductor : pConductor) : pConductor;
  223. FUNCTION SetConductorState(player : pPlayer; state : ULONG; time : LONGINT) : LONGINT;
  224. FUNCTION SetPlayerAttrsA(player : pPlayer; tagList : pTagItem) : BOOLEAN;
  225. PROCEDURE UnlockRealTime(lock : POINTER);
  226.  
  227. IMPLEMENTATION
  228.  
  229. FUNCTION CreatePlayerA(tagList : pTagItem) : pPlayer;
  230. BEGIN
  231.   ASM
  232.     MOVE.L  A6,-(A7)
  233.     MOVEA.L tagList,A0
  234.     MOVEA.L RealTimeBase,A6
  235.     JSR -042(A6)
  236.     MOVEA.L (A7)+,A6
  237.     MOVE.L  D0,@RESULT
  238.   END;
  239. END;
  240.  
  241. PROCEDURE DeletePlayer(player : pPlayer);
  242. BEGIN
  243.   ASM
  244.     MOVE.L  A6,-(A7)
  245.     MOVEA.L player,A0
  246.     MOVEA.L RealTimeBase,A6
  247.     JSR -048(A6)
  248.     MOVEA.L (A7)+,A6
  249.   END;
  250. END;
  251.  
  252. FUNCTION ExternalSync(player : pPlayer; minTime : LONGINT; maxTime : LONGINT) : BOOLEAN;
  253. BEGIN
  254.   ASM
  255.     MOVE.L  A6,-(A7)
  256.     MOVEA.L player,A0
  257.     MOVE.L  minTime,D0
  258.     MOVE.L  maxTime,D1
  259.     MOVEA.L RealTimeBase,A6
  260.     JSR -066(A6)
  261.     MOVEA.L (A7)+,A6
  262.     TST.W   D0
  263.     BEQ.B   @end
  264.     MOVEQ   #1,D0
  265.   @end: MOVE.B  D0,@RESULT
  266.   END;
  267. END;
  268.  
  269. FUNCTION FindConductor(name : pCHAR) : pConductor;
  270. BEGIN
  271.   ASM
  272.     MOVE.L  A6,-(A7)
  273.     MOVEA.L name,A0
  274.     MOVEA.L RealTimeBase,A6
  275.     JSR -078(A6)
  276.     MOVEA.L (A7)+,A6
  277.     MOVE.L  D0,@RESULT
  278.   END;
  279. END;
  280.  
  281. FUNCTION GetPlayerAttrsA(player : pPlayer; tagList : pTagItem) : ULONG;
  282. BEGIN
  283.   ASM
  284.     MOVE.L  A6,-(A7)
  285.     MOVEA.L player,A0
  286.     MOVEA.L tagList,A1
  287.     MOVEA.L RealTimeBase,A6
  288.     JSR -084(A6)
  289.     MOVEA.L (A7)+,A6
  290.     MOVE.L  D0,@RESULT
  291.   END;
  292. END;
  293.  
  294. FUNCTION LockRealTime(lockType : ULONG) : POINTER;
  295. BEGIN
  296.   ASM
  297.     MOVE.L  A6,-(A7)
  298.     MOVE.L  lockType,D0
  299.     MOVEA.L RealTimeBase,A6
  300.     JSR -030(A6)
  301.     MOVEA.L (A7)+,A6
  302.     MOVE.L  D0,@RESULT
  303.   END;
  304. END;
  305.  
  306. FUNCTION NextConductor(previousConductor : pConductor) : pConductor;
  307. BEGIN
  308.   ASM
  309.     MOVE.L  A6,-(A7)
  310.     MOVEA.L previousConductor,A0
  311.     MOVEA.L RealTimeBase,A6
  312.     JSR -072(A6)
  313.     MOVEA.L (A7)+,A6
  314.     MOVE.L  D0,@RESULT
  315.   END;
  316. END;
  317.  
  318. FUNCTION SetConductorState(player : pPlayer; state : ULONG; time : LONGINT) : LONGINT;
  319. BEGIN
  320.   ASM
  321.     MOVE.L  A6,-(A7)
  322.     MOVEA.L player,A0
  323.     MOVE.L  state,D0
  324.     MOVE.L  time,D1
  325.     MOVEA.L RealTimeBase,A6
  326.     JSR -060(A6)
  327.     MOVEA.L (A7)+,A6
  328.     MOVE.L  D0,@RESULT
  329.   END;
  330. END;
  331.  
  332. FUNCTION SetPlayerAttrsA(player : pPlayer; tagList : pTagItem) : BOOLEAN;
  333. BEGIN
  334.   ASM
  335.     MOVE.L  A6,-(A7)
  336.     MOVEA.L player,A0
  337.     MOVEA.L tagList,A1
  338.     MOVEA.L RealTimeBase,A6
  339.     JSR -054(A6)
  340.     MOVEA.L (A7)+,A6
  341.     TST.W   D0
  342.     BEQ.B   @end
  343.     MOVEQ   #1,D0
  344.   @end: MOVE.B  D0,@RESULT
  345.   END;
  346. END;
  347.  
  348. PROCEDURE UnlockRealTime(lock : POINTER);
  349. BEGIN
  350.   ASM
  351.     MOVE.L  A6,-(A7)
  352.     MOVEA.L lock,A0
  353.     MOVEA.L RealTimeBase,A6
  354.     JSR -036(A6)
  355.     MOVEA.L (A7)+,A6
  356.   END;
  357. END;
  358.  
  359. END. (* UNIT REALTIME *)
  360.